/*
 * @Author: YYJ
 * @Date: 2023-12-22 23:41:57
 * @Description: 
 * @FilePath: \lgd-admin\src\pages\study\overview\focusForm.tsx
 */
import React, { FC, useEffect, useState } from 'react';
import { Modal, Form, InputNumber, Input} from 'antd';
import { StudyContentReq } from '@/services/study/content';

interface OperationModalProps {
  open: boolean;
  studyContentReq: Partial<StudyContentReq> | undefined;
  onSubmit: (values: StudyContentReq) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const FocusForm: FC<OperationModalProps> = (props) => {
  const [form] = Form.useForm();
  const [targetIndex, setTargetIndex] = useState(15000);
  const { open, studyContentReq, onCancel, onSubmit } = props;

  useEffect(() => {
    if (form && !open) {
      form.resetFields();
    }
  }, [props.open]);

  useEffect(() => {
    if (studyContentReq) {
      setTargetIndex(studyContentReq.targetIndex as number)
      form.setFieldsValue({
        ...studyContentReq,
        remark: '',
      });
    }
  }, [props.studyContentReq]);

  const handleSubmit = () => {
    if (!form){
      return;
    } 
    form.submit();
  };

  const handleFinish = (values: { [key: string]: any }) => {
    if (onSubmit) {
      onSubmit({ ...values } as StudyContentReq);
    }
  };

  const modalFooter = { okText: '确认', onOk: handleSubmit, onCancel };

  const renderContent = () => {
    return (
      <>
        <Form.Item
          label="名称"
          name="name"
        >
          <Input readOnly/>
        </Form.Item>
        <Form.Item 
          label="完成量"
          name="doneIndex"
        >
          <InputNumber style={{ width: '100%' }} readOnly/>
        </Form.Item>
        <Form.Item 
          label="目标量"
          name="targetIndex"
        >
          <InputNumber style={{ width: '100%' }} readOnly/>
        </Form.Item>
        <Form.Item 
          label="更新量"
          name="updateIndex"
          rules={[{ required: true, message: '请输入更新量' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={1}
            max={targetIndex}
            step={10}
          />
        </Form.Item>
        <Form.Item hidden={true} name="id">
          <Input hidden={true} />
        </Form.Item>
        <Form.Item hidden={true} name="catalogId">
          <Input hidden={true} />
        </Form.Item>
        <Form.Item hidden={true} name="rootCatalogId">
          <Input hidden={true} />
        </Form.Item>
      </>
    );
  };

  return (
    <Modal
      title="更新进度"
      width={640}
      destroyOnClose
      open={open}
      {...modalFooter}
    >
      <Form {...formLayout} form={form} onFinish={handleFinish}>
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default FocusForm